﻿using DataAccess;
using DataEntity;
using IntelligentQuery.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebUI.Controllers
{
    public class StockQueryController : Controller
    {
        //库存查询
        // GET: /StockQuery/
        private DataContext db = new DataContext();

        /// <summary>
        /// 物品表
        /// </summary>
        private IEntityRepository<OffGoods> GoodsRepository;
        public StockQueryController()
        {
            GoodsRepository = new EntityRepository<OffGoods>(db);
        
        }
        /// <summary>
        /// 物品查询首页
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 物品详细页
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult StockView(int id)
        {
            var goods = GoodsRepository.GetByKey(id);

            return PartialView(goods);
        }
        /// <summary>
        /// 查询库存物品数据
        /// </summary>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GoodsJson(int? page, int? rows, string model)
        {
            int Page = page ?? 1;
            int Rows = rows ?? 10;
            QueryModel QModel = null;
            Dictionary<string, object> JsonObj = new Dictionary<string, object>();
            if (!string.IsNullOrWhiteSpace(model))
            {
                QModel = IntelligentQuery.AjaxQueryConverter.ToQueryModel(model);
            }


            var goods = GoodsRepository.Get(QModel).Where(n => !n.BeLock).ToList();

            JsonObj.Add("total", goods.Count());
            JsonObj.Add("rows", goods.OrderByDescending(p => p.ID)
                    .Skip((Page - 1) * Rows)
                   .Take(Rows).Select(s => new { s.ID, s.Name, s.OffGoodsClassID, goodsClassName = s.OffGoodsClass.Name, s.Remark, s.BeLock, s.Inventory, s.AverageAfterTaxPrice,s.AveragePerPrice, s.SortNumber, s.Unit, s.Des, s.Code }));


            return JsonHelper.JsonEx(JsonObj);
        }

        /// <summary>
        /// 查询库存所有物品数据
        /// </summary>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GoodsAllJson( string model)
        {
          
            QueryModel QModel = null;
            Dictionary<string, object> JsonObj = new Dictionary<string, object>();
            if (!string.IsNullOrWhiteSpace(model))
            {
                QModel = IntelligentQuery.AjaxQueryConverter.ToQueryModel(model);
            }


            var goods = GoodsRepository.Get(QModel).Where(n => !n.BeLock).ToList();

            JsonObj.Add("total", goods.Count());
            JsonObj.Add("rows", goods.OrderByDescending(p => p.ID)
                   .Select(s => new { s.ID, s.Name, s.OffGoodsClassID, goodsClassName = s.OffGoodsClass.Name, s.Remark, s.BeLock, s.Inventory, s.AverageAfterTaxPrice, s.AveragePerPrice, s.SortNumber, s.Unit, s.Des, s.Code }));


            return JsonHelper.JsonEx(JsonObj);
        }
    }
}
